package cn.jimmie.learn.viewArt.weight

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Path
import android.os.Build
import android.support.annotation.RequiresApi
import android.support.v4.content.ContextCompat
import android.util.AttributeSet
import cn.jimmie.learn.viewArt.R

/**
 * function :
 *
 * <p> Create Time: 2019年01月17日 00:57 </p>
 * <p> @author Jimmie.Qian  </p>
 */
class RoundImageView(context: Context, attrs: AttributeSet) : BaseView(context, attrs) {

    private val image = ContextCompat.getDrawable(context, R.drawable.layer1)!!
    private val path = Path()

    init {
        image.setBounds(0, 0, w, h)
        val radius = Math.min(w, h) / 2f
        path.addCircle(w / 2f, h / 2f, radius, Path.Direction.CCW)
    }

    override fun onDraw(canvas: Canvas) {
        image.draw(canvas)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            canvas.clipOutPath(path)
        }
        canvas.drawColor(Color.WHITE)
    }
}